<?php

class Smtp_mail
{
    private $host; //保存需要连接的SMTP服务器
    private $port = 25; //绑定端口
    private $user; //登录smtp服务器用户名
    private $pass; //登录smtp服务器密码
    private $debug = false;
    private $sock; //保存于smtp服务器链接的句柄
    private $mail_format = 0; //标志使用什么格式发送邮件，0为普通文本，1为HTML邮件

    public function __construct($host, $port, $user, $pass, $format = 1, $debug = 1)
    {
        $this->host        = $host;
        $this->port        = $port;
        $this->user        = base64_encode($user);
        $this->pass        = base64_encode($pass);
        $this->mail_format = $format;
        $this->debug       = $debug;
        $this->sock        = fsockopen($this->host, $this->port, $errno, $errstr, 10);
        if (!$this->sock) {
            exit("错误代码：$errno,错误信息：$errstr");
        }
        $response = fgets($this->sock);
        if (strstr($response, "220") === false) {
            exit("server error:$response\n");
        }
    }
    private function show_debug($message)
    {
        if ($this->debug) {
            echo "<p>Debug:$message</p>";
        }
    }
    private function do_command($cmd, $return_code)
    {
        fwrite($this->sock, $cmd);
        $response = fgets($this->sock);
        if (strstr($response, "$return_code") === false) {
            $this->show_debug($response);
            return false;
        }
        return true;
    }
    private function is_email($email)
    {
        $pattren = "/^[^_][\w]*@[\w.]+[\w]*[^_]$/";
        if (preg_match($pattren, $email, $matches)) {
            return true;
        } else {
            return false;
        }
    }
    public function send_mail($from, $to, $subject, $body)
    {
        if (!$this->is_email($from) || !$this->is_email($to)) {
            $this->show_debug("Please enter vaild from/to email.");
            return false;
        }
        if (empty($subject) || empty($body)) {
            $this->show_debug("Please enter subject/content.");
            return false;
        }
        $detail = "From:$from\r\n";
        $detail .= "To:$to\r\n";
        $detail .= "Subject:$subject\r\n";
        if ($this->mail_format == 1) {
            $detail .= "Content-Type:text/html;\r\n";
        } else {
            $detail .= "Content-Type:text/plain;\r\n";
        }
        $detail .= "charset=utf-8\r\n\r\n";
        $detail .= $body;
        //根据SMTP协议向服务器发送命令。
        $this->do_command("HELO ssl://smtp.qq.com\r\n", 250);
        $this->do_command("AUTH LOGIN\r\n", 334);
        $this->do_command("$this->user\r\n", 334);
        $this->do_command("$this->pass\r\n", 235);
        $this->do_command("MAIL FROM:<$from>\r\n", 250);
        $this->do_command("RCPT TO:<$to>\r\n", 250);
        $this->do_command("DATA\r\n", 354);
        $this->do_command($detail . "\r\n.\r\n", 250);
        $this->do_command("QUIT\r\n", 221);
        return true;
    }
}
$Smtp_mail = new Smtp_mail("ssl://smtp.qq.com", 465, "977564830@qq.com", "xjkuymfzkspfbdjg");
$Smtp_mail->send_mail("977564830@qq.com", "chenyu977564830@163.com", "测试邮件发送", "this is example email for you");
